<template>
  <a-modal title="货位详情" title-align="start" :footer="false" :mask-closable="false" v-model:visible="showvisible" @cancel="close" :width="900">
    <a-descriptions :data="desData" :column="2" bordered />
    <div class="mt10">
      <FilterTable :filterItem="filterItem" :isReset="true" :is-show-tag="false" @search="searchByVal" />
    </div>
    <a-space>
      <div>已选中</div>
      <div>{{ ids.length }}条</div>
      <a-button class="gray-outline" @click="remove">移除</a-button>
    </a-space>
    <a-table size="mini" class="table-two-line mt10" ref="table" @sorter-change="sortChange" :loading="loading" row-key="id" v-model:selected-keys="ids" :row-selection="{ type: 'checkbox', showCheckedAll: true, width: 40 }" :data="tableList" :pagination="false" :scroll="{ x: '100%', y: 400 }">
      <template #columns>
        <a-table-column title="序号" #cell="{ record, rowIndex }" :width="40">
          {{ rowIndex + 1 }}
        </a-table-column>
        <a-table-column title="仓库名称" #cell="{ record, rowIndex }" :width="40">
          {{ record.warehouseName }}
        </a-table-column>
        <a-table-column title="店铺" :width="40">
          <template #cell="{ record }">
            {{ record.shopName }}
          </template>
        </a-table-column>
        <a-table-column title="图片" :width="60">
          <template #cell="{ record }">
            <ImgPopover :src="record.productImg">
              <template #con>
                <a-image :preview="false" width="40" height="40" fit="contain" :src="record.productImg" />
              </template>
            </ImgPopover>
          </template>
        </a-table-column>
        <a-table-column title="产品名称/SKU" :width="80">
          <template #cell="{ record }">
            <div>{{ record.productName ? record.productName : '--' }}</div>
            <div>{{ record.sku ? record.sku : '--' }}</div>
          </template>
        </a-table-column>
        <a-table-column title="MSKU/FNSKU" :width="80">
          <template #cell="{ record }">
            <div>{{ record.msku ? record.msku : '--' }}</div>
            <div>{{ record.fnsku ? record.fnsku : '--' }}</div>
          </template>
        </a-table-column>
        <a-table-column title="库存属性" :width="40">
          <template #cell="{ record }">
            {{ record.attribute == 'good' ? '良品' : '次品' }}
          </template>
        </a-table-column>
        <a-table-column title="可用量" data-index="amtAvailable" :sortable="{ sortDirections: ['ascend', 'descend'] }" :width="60">
          <template #cell="{ record }">
            {{ record.amtAvailable }}
          </template>
        </a-table-column>
      </template>
    </a-table>
  </a-modal>
</template>

<script lang="ts" setup>
  import { computed, ref, onMounted } from 'vue'
  import { Message, Modal } from '@arco-design/web-vue'
  import { getCategoryList } from '@/api/product'
  import { findInventoryPage, inventoryRemoveLocation } from '@/api/warehouse'
  import FilterTable from '@/components/TableFilter.vue'
  import _ from 'lodash-es'

  let props = defineProps(['modelValue', 'info'])
  let emits = defineEmits(['update:modelValue', 'save'])
  let ids = ref([])
  let searchValue: any = ref({})
  let tableList = ref([])
  let loading = ref(false)
  let desData = ref([
    {
      label: '仓库：',
      value: 'Socrates',
    },
    {
      label: '货位状态：',
      value: '123-1234-1234',
    },
    {
      label: '货架：',
      value: 'Beijing',
    },
    {
      label: '货位：',
      value: 'Beijing',
    },
  ])
  const filterItem = ref([
    {
      key: 'shopName',
      keyWidth: '110',
      label: '店铺名称',
      width: 140,
      value: ref(),
      type: 'input_search',
    },
    {
      type: 'input_keyword',
      key: 'skus',
      keyItems: [
        { label: 'SKU', value: 'skus' },
        { label: 'SPU', value: 'spus' },
        { label: 'MSKU', value: 'mskus' },
        { label: 'FNSKU', value: 'fnskus' },
        { label: '产品名称', value: 'productName' },
      ],
      keyWidth: '80',
      value: ref(),
      placeholder: '请输入',
      label: 'SKU',
      width: 140,
      isMultiple: true,
    },
    {
      type: 'cascader',
      key: 'cateId',
      optionItems: [],
      keyWidth: '120',
      value: ref(),
      placeholder: '请选择',
      label: '类目',
      width: 180,
      isMultiple: false,
    },
    {
      key: 'attribute',
      label: '库存属性',
      value: ref(),
      type: 'select',
      width: 140,
      optionItems: [
        { label: '良品', value: 'good' },
        { label: '次品', value: 'defective' },
      ],
    },
  ])

  let showvisible: any = computed({
    set(val) {
      emits('update:modelValue', val)
    },
    get() {
      return props.modelValue
    },
  })

  const initTableInfo = (info: any) => {
    desData.value[0].value = info.warehouseName
    desData.value[1].value = info.status == 1 ? '占用' : '空闲'
    desData.value[2].value = info.shelvesCode
    desData.value[3].value = info.locationCode
  }

  const getCategory = async () => {
    const res: any = await getCategoryList()
    filterItem.value[2].optionItems = res.data
    // categoryOptions.value = res.data
  }

  const getSkuList = async () => {
    let params = {
      pageSzie: 200,
      ...searchValue.value,
      locationId: props.info.id,
    }
    loading.value = true
    const res: any = await findInventoryPage(params)
    if (res) {
      tableList.value = res.data?.records || []
    }
    loading.value = false
  }

  const sortChange = (item: any, extra: any) => {
    if (extra) {
      searchValue.value.sortField = item
      searchValue.value.sortType = extra == 'ascend' ? '1' : '-1'
    } else {
      searchValue.value.sortField = ''
      searchValue.value.sortType = ''
    }
    getSkuList()
  }

  const searchByVal = (info: any) => {
    searchValue.value = info
    getSkuList()
  }

  const remove = async () => {
    if (!ids.value[0]) return Message.error('请选择数据')
    let params = {
      id: props.info.id,
      inventoryIds: ids.value,
    }
    const res: any = await inventoryRemoveLocation(params)
    if (res) {
      Message.success('操作成功')
      getSkuList()
    }
  }

  const close = () => {
    emits('save')
    showvisible.value = false
  }

  onMounted(() => {
    getSkuList()
    initTableInfo(props.info)
    getCategory()
  })
</script>

<style lang="less" scoped>
  :deep(.arco-upload-picture-card) {
    min-width: 40px;
    height: 40px;
  }
  :deep(.arco-upload-list-picture) {
    width: 40px;
    height: 40px;
  }

  :deep(.arco-upload-list-picture-operation) {
    line-height: 40px;
  }

  .step-item {
    height: 14px;
    line-height: 14px;
  }

  .tab-box {
    display: flex;
    li {
      height: 30px;
      width: 60px;
      text-align: center;
      line-height: 30px;
      border: 1px solid #d7d7d7;
      border-right: none;
      &:last-child {
        border-right: 1px solid #d7d7d7;
      }
      font-size: 12px;
    }
    .active {
      background: #165dff;
      color: white;
    }
  }
</style>
